﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Collections;

namespace Reddit.Data
{
    [DataContract]
    public class Listing<T> : DataContainer<ChildrenContainer<T>>, IEnumerable<T> 
    {
        private T[] _children;
        protected T[] children
        {
            get
            {
                if (_children != null)
                {
                    return _children;
                }
                else if (this.data == null)
                {
                    return _children = new T[] { };
                }
                else
                {
                    return _children = this.data.children;
                }
            }
        }

        public T this[int i]
        {
            get
            {
                return this.children[i];
            }
        }

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return this.children.AsEnumerable<T>().GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.children.GetEnumerator();
        }

        #endregion
    }
}
